//
//  SPManageListController.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit
import WMZDialog
class SPManageListController: SPBaseController {
   
   
    @IBOutlet var tabTop: NSLayoutConstraint!
    @IBOutlet var resultLabel: UILabel!
    @IBOutlet var searchResultView: UIView!
    @IBOutlet var tableView: UITableView!
    lazy var viewModel: SPManageViewMode = {
        return SPManageViewMode()
    }() 
    var searchStr = ""
//    SPManageSearchView
    lazy var searchView: SPManageSearchView = {
        let view = Bundle.main.loadNibNamed("SPManageSearchView", owner: self, options: nil)?.first as! SPManageSearchView
        view.frame = keyWindow!.frame
        return view
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.searchStr = ""
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        setTab()
        // Do any additional setup after loading the view.
    }
    @IBAction func tapSearchAction(_ sender: Any) {
        self.searchView.onSave = { [self] dict in
            let values = concatenateDictionaryValues(dict)
            if values.count > 0{
                self.resultLabel.text = values
                self.searchResultView.isHidden = false
                self.tabTop.constant = 90
            }else{
                self.searchResultView.isHidden = true
                self.tabTop.constant = 10
            }
            print(values)
            self.searchStr = concatenateDictionary(dict)
            self.loadData()
        }
        keyWindow?.addSubview(self.searchView)
    }
    func concatenateDictionaryValues(_ dictionary: [String: String]) -> String {
        var result = ""
        
        for (key, value) in dictionary {
            if !value.isEmpty {
                if !result.isEmpty {
                    result += "; "
                }
                
                if key == "date" {
                    result += "\(value)"
                } else {
                    result += "\(value)"
                }
            }
        }
        
        return result
    }



    func concatenateDictionary(_ dictionary: [String: String]) -> String {
        var result = ""
        
        for (key, value) in dictionary {
            if !value.isEmpty {
                if !result.isEmpty {
                    result += " and "
                }
                
                if key == "name" {
                    result += "\(key) LIKE '%\(value)%'"
                } else {
                    result += "\(key) = '\(value)'"
                }
            }
        }
        
        return result
    }


    
    @IBAction func newAnniversaryAction(_ sender: Any) {
        self.navigationController?.pushViewController(SPManageAddController(), animated: true)
    }
    
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "SPManageListCell", bundle: .main), forCellReuseIdentifier: "SPManageListCell")
    }
    func loadData(){
        self.viewModel.getPageData(self.searchStr) { models in
            print(self.viewModel.lately)
            self.tableView.reloadData()
        }
        
    }
}
extension SPManageListController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:SPManageListCell = tableView.dequeueReusableCell(withIdentifier: "SPManageListCell") as! SPManageListCell
        let model = self.viewModel.dataSource[indexPath.row]
        cell.model = model
        cell.onDeleted = {
            model.deleteToDB()
            self.loadData()
        }
        cell.onCompleted = {
            model.isCompleted = true
            model.updateToDB()
            self.loadData()
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
